package BeenChina.PM.Action;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import BeenChina.PM.Model.ModelNavigation;
import BeenChina.PM.Model.ModelNavigationItem;
import BeenChina.PM.Model.ModelUser;
import BeenChina.PM.Model.ModelUserRole;
import BeenChina.PM.Service.ServiceFactory;
import BeenChina.PM.Util.Permission;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class MainPageAction extends ActionSupport 
{	
	private static final long serialVersionUID = 1L;
	private ModelUser user;
 
	
	public String getSysInfo(){
		return user.getUserName();
	}
	
	public ModelUser getUser(){
		return this.user;
	}	 

	private List<ModelNavigation> navigations;
	public List<ModelNavigation> getNavigations(){
		return navigations;
	}
	
	
	@Override
	public String execute() {
		try{
			getNavigationMenu();
			return Action.SUCCESS;
		}
		catch(Exception exp){
			return Action.ERROR;
		}
	}
	
 
	private  void getNavigationMenu(){
		 
		//HttpServletRequest request = ServletActionContext.getRequest();
		//user = CookieUtil.getCookie(request);
		ModelUser user = (ModelUser) ActionContext.getContext().getSession().get("user");
		 //todo get one user from db
		//if (user!= null){
		this.user = ServiceFactory.getInstance().getSystemInfoService().selectUser(user.getUserID());
		//}
		//save all roles permission
		String userPermissions ="";
		
		List<ModelUserRole> userRoles = ServiceFactory.getInstance().getSystemInfoService().selectUserRoles(user.getUserID());		
		if (userRoles != null){
			for (ModelUserRole modelUserRole : userRoles) {				
				String permissionIds = modelUserRole.getPermissionsString();
				EnumSet<Permission> permissions = Permission.getPremissions(permissionIds);
				
				if (permissions!=null){
					
					//save permissionids
					if (userPermissions.length()>0)
						userPermissions+= ",";
					userPermissions += permissionIds;
					
					for (Permission p : permissions) {						 
						String navId = p.getItem().getMenu().getItem().getId();
						String navName = p.getItem().getMenu().getItem().getName();
						
						ModelNavigation nav =new ModelNavigation(navId,navName);						
						if (navigations!=null){
						  Integer index =navigations.indexOf(nav);
						  if (index >= 0){
							  nav = navigations.get(index);
						  }
						  else {
							  navigations.add(nav);
						  }						 
						}
						else {
							navigations = new ArrayList<ModelNavigation>();
							navigations.add(nav);
						}
						
						ModelNavigationItem navItem = new ModelNavigationItem();
						navItem.setId(p.getItem().getId());
						navItem.setName(p.getItem().getName());
						navItem.setAction(p.getItem().getAction());
						navItem.setIcoName(p.getItem().getIco());
						navItem.setMemo(p.getItem().getMemo());
						nav.addItems(navItem);												
					}
				}
			}
		}
		
		//update session
		if (userPermissions.length()>0){
			 
			((ModelUser) ActionContext.getContext().getSession().get("user")).setAllpermissions(userPermissions.split(","));
		}
		else {
			((ModelUser) ActionContext.getContext().getSession().get("user")).setAllpermissions(null);
		}
		
		
 
	}	
}
